package com.twu.biblioteca;

import com.twu.biblioteca.Entity.User;
import com.twu.biblioteca.Model.UserList;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

public class LoginService implements Service {
    private Map<String, User> list;
    private UserList userList;

    public LoginService(UserList userList) {
        this.userList = userList;
        this.list = userList.getUserList();
    }

    @Override
    public String getServiceDescription() {
        return null;
    }

    @Override
    public Menu getMenu() {
        return null;
    }

    @Override
    public void doService() {
        System.out.println("Please login the system:\n" + "User ID:");
        String userID = getInput();
        System.out.println("Password:");
        String password = getInput();
        String result = Login(userID, password);
        System.out.println(result);
    }

    public static String getInput() {
        BufferedReader selection = new BufferedReader(new InputStreamReader(System.in));
        try {
            return selection.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "That book is not available";
    }

    public String Login(String id, String password) {

        if (list.containsKey(id) && list.get(id).getPassword().equals(password)) {
            userList.setLoginUser(list.get(id));
            return "Login Success!";
        } else return "Login Failed!";
    }
}
